﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SqlServer.Server;

namespace DBModel
{
    public partial class User
    {
        public override string ToString()
        {
            return string.Format("{0,3} {1,15} {2,15}", UserId, UserName, Password);
        }
    }

    public class UserDB
    {
        public static User AuthenticateUser(string userName, string password)
        {
            try
            {
                return DB.Context.Users.SingleOrDefault(u => u.UserName == userName && u.Password == password && !u.IsDeleted);
            }
            catch
            {
                return null;
            }
        }

        public static bool IsUserNameAvailable(string username)
        {
            var usersWithName = DB.Context.Users.Where(u => u.UserName == username).ToList();
            return !usersWithName.Any();
        }
    }
}
